package com.zmn.oms.admin.controller.price;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.zmn.base.price.dubbo.interfaces.item.ItemRemoteService;
import com.zmn.base.price.dubbo.interfaces.tariff.TariffSkuPriceRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.utils.MoneyUtil;
import com.zmn.oms.model.entity.pricefeedback.PriceFeedback;
import com.zmn.oms.model.entity.pricefeedback.PriceFeedbackDRO;
import com.zmn.oms.model.entity.pricefeedback.PriceFeedbackQuery;
import com.zmn.oms.services.interfaces.pricefeedback.PriceFeedbackService;
import com.zmn.tapi.common.export.model.dio.AddExportTaskDIO;
import com.zmn.tapi.dubbo.DubboConsts;
import com.zmn.tapi.dubbo.interfaces.export.ExportModifyRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: 价格反馈查询控制层
 *
 * @author weilai
 * @since 2022/9/13  9:30
 */
@RestController
@RequestMapping("/price/feedback")
@Slf4j
public class PriceFeedbackListController {
    // 价格产品服务项分组key
    private static final String TARIFF_PRODUCT_ITEM = "tariff.id:%s:product.id:%s:item.id:%s";

    @Resource
    private PriceFeedbackService priceFeedbackService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ExportModifyRemoteService picUploadRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private TariffSkuPriceRemoteService tariffSkuPriceRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private ItemRemoteService itemRemoteService;

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("price/feedback/list");
        return mv;
    }

    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(PriceFeedbackQuery priceFeedbackQuery) {
        String feedbackDateRange = priceFeedbackQuery.getFeedbackDateRange();
        if (StringUtils.isNotBlank(feedbackDateRange)) {
            String[] dateRange = feedbackDateRange.split("/");
            priceFeedbackQuery.setFeedbackStartDate(DateUtil.parseDate(dateRange[0]));
            priceFeedbackQuery.setFeedbackEndDate(DateUtil.endOfDay(DateUtil.parseDate(dateRange[1])));
        }
        List<PriceFeedback> priceFeedbacks = priceFeedbackService.listPageByQuery(priceFeedbackQuery);
        // 获取当前价格
        List<PriceFeedbackDRO> priceFeedbackDROS = BeanUtil.copyToList(priceFeedbacks, PriceFeedbackDRO.class);

        // 将分转化为元
        this.conversionFenToYuan(priceFeedbackDROS);
        LayuiTableDO<PriceFeedbackDRO> data = new LayuiTableDO<>();
        data.setCount(priceFeedbackQuery.getTotalCount());
        data.setData(priceFeedbackDROS);
        return ResultDTO.success(data);
    }


    /**
     * 导出excel
     *
     * @param query
     */
    @RequestMapping(value = "/exportData")
    @ResponseBody
    public void exportData(@ModelAttribute PriceFeedbackQuery query) {
        // 查询数据
        query.setPageSize(10000);
        List<PriceFeedback> priceFeedbacks = priceFeedbackService.listPageByQuery(query);
        if (CollectionUtil.isNullOrEmpty(priceFeedbacks)) {
            log.info("没查询到价格反馈数据,入参:{}", JSON.toJSONString(query));
            return;
        }
        List<PriceFeedbackDRO> priceFeedbackDROS = BeanUtil.copyToList(priceFeedbacks, PriceFeedbackDRO.class);
        // 将分转化为元
        this.conversionFenToYuan(priceFeedbackDROS);
        query.setExcelCreater(StaffAdminService.currentStaff().getRealName());
        query.setStaffId(StaffAdminService.currentStaff().getStaffId());

        AddExportTaskDIO addExportTaskDIO = new AddExportTaskDIO();
        addExportTaskDIO.setCreater(query.getExcelCreater());
        addExportTaskDIO.setExportName("价格反馈");
        addExportTaskDIO.setExportType("文件类型");
        addExportTaskDIO.setFileType(".xlsx");
        addExportTaskDIO.setStaffId(query.getStaffId());
        // 创建下载任务ID
        ResponseDTO<Integer> responseDTO = picUploadRemoteService.addExportTask(addExportTaskDIO);
        // 获取下载文件表头
        List<List<String>> listData = this.getOneRowTitle();

        for (PriceFeedbackDRO priceFeedback : priceFeedbackDROS) {
            List<String> title = new ArrayList<>();
            title.add(priceFeedback.getFeedbackId() + "");
            title.add(priceFeedback.getEngineerId() + "");
            title.add(priceFeedback.getCityName());
            title.add(priceFeedback.getChannelName());
            title.add(priceFeedback.getErpServCateg());
            title.add(priceFeedback.getErpCategOne());
            title.add(priceFeedback.getErpCategTwo());
            title.add(priceFeedback.getErpProductName());
            title.add(priceFeedback.getErpProductId() + "");
            title.add(priceFeedback.getProjectName());
            title.add(priceFeedback.getProjectId() + "");
            title.add(priceFeedback.getProductType());
            title.add(priceFeedback.getBrand());
            title.add(priceFeedback.getTariffId() + "");
            title.add(priceFeedback.getTariffName());
            title.add(priceFeedback.getTariffCalMode());
            title.add(priceFeedback.getFeedbackPriceStr());
            title.add(priceFeedback.getExpectPriceStr());
            title.add(priceFeedback.getRemark());
            title.add(DateUtil.format(priceFeedback.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            listData.add(title);
        }

        // 下载文件
        ResponseDTO<Boolean> rd = picUploadRemoteService.exportInfo(responseDTO.getData(), listData);
    }

    private List<List<String>> getOneRowTitle() {
        List<List<String>> listData = new ArrayList<>();
        List<String> title = new ArrayList<>();
        title.add("ID");
        title.add("工程师Id");
        title.add("所在城市");
        title.add("渠道名称");
        title.add("后台服务分类");
        title.add("后台一级分类");
        title.add("后台二级分类");
        title.add("后台产品名称");
        title.add("后台产品ID");
        title.add("项目名称");
        title.add("项目ID");
        title.add("产品类型");
        title.add("品牌");
        title.add("价格表ID");
        title.add("价格表名称");
        title.add("定价方式");
        title.add("反馈执行价");
        title.add("期望价格");
        title.add("备注说明");
        title.add("反馈时间");

        listData.add(title);
        return listData;
    }

    /**
     * 将分转为元
     *
     * @param priceFeedbackDROS
     */
    private void conversionFenToYuan(List<PriceFeedbackDRO> priceFeedbackDROS) {
        priceFeedbackDROS.stream().forEach(priceFeedback -> {
            priceFeedback.setFeedbackPriceStr(MoneyUtil.longParseToYuanStr((long) priceFeedback.getFeedbackPrice()));
            priceFeedback.setExpectPriceStr(MoneyUtil.longParseToYuanStr((long) priceFeedback.getExpectPrice()));
        });
    }
}
